
#define NUM 10
enum
{
    OK = 0,
    ERR
};

class ThreadES
{
public:
    ThreadES(const string &name, int id, time_t createTime, int top)
        : _name(name),
          _id(id),
          _createTime((uint64_t)createTime),
          _status(OK),
          _top(top),
          _res(0)
    {
    }
    ~ThreadES()
    {
    }

public:
    // 输入的
    string _name;
    int _id;
    uint64_t _createTime;

    // 返回的
    int _status;
    int _top;
    int _res;
};

void *pthread_run1(void *args)
{
    ThreadES *tid = static_cast<ThreadES *>(args);

    for (int i = 1; i <= tid->_top; i++)
    {
        tid->_res += i;
    }
    cout << tid->_name << " 已完成计算任务!" << endl;

    pthread_exit(tid);
    return tid;
}

int main()
{

    pthread_t ids[NUM];

    for (int i = 0; i < NUM; i++)
    {
        char arr[64];
        snprintf(arr, 64, "线程-%d", i + 1);
        ThreadES *tid = new ThreadES(arr, i + 1, time(nullptr), 100 + 5 * i);
        pthread_create(ids + i, nullptr, pthread_run1, tid); // 这里也可以传对象
        sleep(1);
    }

    void *ret = nullptr;

    // 线程等待 pthread_join
    for (int i = 0; i < NUM; i++)
    {
        int n = pthread_join(ids[i], &ret);
        if (n != 0)
            cerr << "pthread_join false" << endl;
        ThreadES *tid = static_cast<ThreadES *>(ret); // 基本的类型转换，不能用与指针（void* 除外）
        if (tid->_status == OK)
        {
            cout << tid->_name << "计算结果：" << tid->_res << "计算任务：[1," << tid->_top << "]" << endl;
        }

        delete tid;
    }

    cout << "thread quit ....." << endl;
    return 0;
}